package org.platform.processor.diagnose.job;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mc.mic.core.log.bus.BusLogPusher;
import com.mc.mic.core.log.frame.LogBo;
import lombok.extern.slf4j.Slf4j;
import org.platform.constant.LogType;
import org.platform.core.JobManager;
import org.platform.dao.mapper.FdDiagnoseRecordMapper;
import org.platform.dao.mapper.FdDiagnoseTaskResultTestMapper;
import org.platform.dao.mapper.FdStepInfoMapper;
import org.platform.dao.mapper.FdTaskProcessDataMapper;
import org.platform.mo.bussmo.diagnose.test.TestResultContent;
import org.platform.mo.bussmo.log.DiagnoseRecordMqBO;
import org.platform.mo.bussmo.log.TaskStepMqBO;
import org.platform.mo.bussmo.log.TestDiagnoseResultMqBO;
import org.platform.mo.csmo.common.FD_TestDiagnoseResult;
import org.platform.mo.csmo.common.StepInfo;
import org.platform.mo.csmo.common.TaskProcessData;
import org.platform.mo.db.FdDiagnoseRecord;
import org.platform.mo.db.FdDiagnoseTaskResultTest;
import org.platform.mo.db.FdStepInfo;
import org.platform.mo.db.FdTaskProcessData;
import org.platform.processor.diagnose.DiagnoseResultProcessor;
import org.platform.tool.util.BeansUtils;
import org.platform.tool.util.JsonUtils;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Random;

/**
 * todo 仅仅 用于测试.后面要删除的
 *
 * @author FengJie
 * @date 2018/7/23
 */
@Slf4j
@Component
public class TestPromoteJob implements InterruptableJob {
    @Autowired
    private BusLogPusher mqLog;
    @Autowired
    private FdDiagnoseRecordMapper recordMapper;
    @Autowired
    private FdTaskProcessDataMapper processDataMapper;
    @Autowired
    private FdStepInfoMapper stepInfoMapper;
    @Autowired
    private DiagnoseResultProcessor diagnoseResultProcessor;
    @Autowired
    private FdDiagnoseTaskResultTestMapper resultTestMapper;
    @Autowired
    private JobManager job;


    @Override
    public void interrupt() {

    }

    @Override
    public void execute(JobExecutionContext context) {
        try {
            promoteTaskLog();
            promoteTaskStep();
            promoteTaskResult();
        } catch (JsonProcessingException e) {
            log.error("推送日志信息定时器错误,异常信息:", e);
        }
    }

    /**
     * 测试日志
     */
    private void promoteTaskLog() {
        List<FdDiagnoseRecord> recordList = recordMapper.selectAll();
        FdDiagnoseRecord record = recordList.size() > 1 ? recordList.get(recordList.size() - 1) : new FdDiagnoseRecord();
        mqLog.log(new LogBo<>(LogType.DIAGNOSE_RECORD, new DiagnoseRecordMqBO(record)));

    }

    /**
     * 推送测试步骤
     */
    private void promoteTaskStep() {
        List<FdTaskProcessData> processDataList = processDataMapper.selectAll();
        FdTaskProcessData processData = processDataList.get(getRandom(processDataList.size() - 1));
        TaskProcessData taskProcessData = new TaskProcessData();
        BeanUtils.copyProperties(processData, taskProcessData);
        List<FdStepInfo> stepList = stepInfoMapper.selectAll();
        List<StepInfo> stepInfo = BeansUtils.copyListProperties(stepList, StepInfo.class);
        taskProcessData.setStepInfoList(stepInfo);
        mqLog.log(new LogBo<>(LogType.TASK_STEP, new TaskStepMqBO(taskProcessData)));
    }

    /**
     * 推送测试结果
     */
    private void promoteTaskResult() throws JsonProcessingException {
        List<FdDiagnoseTaskResultTest> list = resultTestMapper.selectAll();
        String diagnoseId = list.get(getRandom(list.size() - 1)).getDiagnoseid();

        TestResultContent result = diagnoseResultProcessor.queryTestDiagnoseResult(diagnoseId);
        String jsonResult = JsonUtils.beanToJson(result, true);
        mqLog.log(new LogBo<>(LogType.TEST_RESULT, new TestDiagnoseResultMqBO(new FD_TestDiagnoseResult(jsonResult))));

    }

    private int getRandom(int max) {
        int min = 0;
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }

    @PostConstruct
    void init() {
        JobDetailImpl jd = new JobDetailImpl();
        jd.setJobClass(TestPromoteJob.class);
        jd.setKey(JobKey.jobKey("TestPromoteJob"));

        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(10)
                        .repeatForever())
                .startNow()
                .build();

        job.register(jd, trigger);
    }

}
